<template>
  <div class="flex flex-col h-full">
    <SearchContainer @reset="() => formRef.resetFields()" @search="getData">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="xmlb" label="项目类别" class="w-64">
          <a-select v-model:value="formState.xmlb" max-tag-count="responsive" :options="options" mode="multiple" placeholder="请选择" />
        </a-form-item>

        <a-form-item name="ylfl" label="药理分类" class="w-64 ml-2">
          <SelectItem
            v-model:value="formState.ylfl"
            type="DT00026"
            mode="multiple"
            value-key="id"
            max-tag-count="responsive"
            placeholder="请选择"
            allow-clear
          />
        </a-form-item>

        <a-form-item name="zjm" class="w-64 ml-2">
          <a-input v-model:value="formState.zjm" placeholder="药品编号/药品名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3 flex justify-end">
        <BaseExport type="运维" name="西成药字典" code="00001062" :search-params="formState">导出</BaseExport>

        <!-- <a-button class="ml-2">导出</a-button> -->
      </div>
      <BaseTable
        :scroll="{ x: 3600 }"
        column-code="00001062"
        fill
        :data-source="tableData"
        bordered
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex == 'yxztStr'">
            <a-switch v-model:checked="record.yxzt" checked-value="0" un-checked-value="1" @change="onHandleDeactivate(record, $event)" />
          </template>
          <template v-if="column.dataIndex == 'sfpj'">
            {{ record.sfpj == 0 ? '否' : '是' }}
          </template>
          <template v-if="column.dataIndex == 'otcFlag' && record.otcFlag !== undefined">
            {{ record.otcFlag ? 'Rx处方药' : 'OTC非处方药' }}
          </template>
          <template v-if="column.dataIndex == 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
          <template v-if="column.dataIndex == 'psbz'">
            {{ record.psbz == 0 ? '否' : '是' }}
          </template>
          <template v-if="column.dataIndex == 'sffywz'">
            {{ record.sffywz == 0 ? '否' : '是' }}
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'
import SelectItem from '@/components/SelectItem'

const { empName } = useUserStore()
const loading = ref(false)
const formRef = ref(null)
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const formState = ref({
  xmlb: [],
  cxxm: 1,
  ylfl: []
})
const tableData = ref([])
const options = ref([])

const getData = async () => {
  loading.value = true
  const { code, data, total, msg } = await post('/operation/opex/getDrugInfoList', {
    pageNum: page.value.current,
    pageSize: page.value.pageSize,
    ...formState.value
  })
  loading.value = false
  if (code !== 0) return message.error(msg)
  page.value.total = total
  tableData.value = data.map((item) => {
    item.yxzt = item.yxzt ? '1' : '0'
    return item
  })
}
const getProjectList = async () => {
  const { code, data } = await post('/drugStorage/comprehenive/getProjectList')
  if (code !== 0) return
  options.value = data.map((item) => {
    return {
      label: item.mc,
      value: item.bh
    }
  })
}
const onHandleDeactivate = async (item, number) => {
  item.yxzt = number
  const { code, msg } = await post('/operation/opex/doModifyDrugStatus', {
    ...item,
    czr: empName,
    cztime: new Date()
  })
  if (code === 0) {
    message.success(msg)
    getData()
  }
}

const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  getData()
}
onMounted(() => {
  getData()
  getProjectList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
